package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.Checkstand.WXPay;
import com.banzhi.shop.model.CallBack.CheckStandCallBack;
import com.banzhi.shop.model.ICheckStandModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/11/10.
 */

public class ChoiceCheckStandImpl implements ICheckStandModel {
    @Override
    public void PayYure(String orderid, String orderprice, final CheckStandCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceBalancePayUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("orderid", orderid);
        params.addBodyParameter("orderprice", orderprice);
        LogUtils.d("PayYure", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        callBack.onPayYureSuccess(jsonobject);
                    } else {
                        callBack.onPayYureError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayYure", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(32ad)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void PayWX(String out_trade_no, String total_fee, final CheckStandCallBack callBack) {
        final RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoicePaywxUrl);
        params.setMethod(HttpMethod.POST);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");//Content-Type     application/x-www-form-urlencoded
        params.addBodyParameter("out_trade_no", out_trade_no);
        params.addBodyParameter("total_fee", total_fee);
        LogUtils.d("PayWX", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        WXPay pay = gson.fromJson(result, WXPay.class);
                        callBack.onPayWXSuccess(pay.getData());
                    } else {
//                        callBack.onPayWXSuccess(jsonobject);
                        callBack.onPayWXError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayWX", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(36sa)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void PayZFB(String out_trade_no, String total_fee, final CheckStandCallBack callBack) {
        final RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoicePayzfbUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("out_trade_no", out_trade_no);
        params.addBodyParameter("total_fee", total_fee);
        LogUtils.d("PayZFB", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
//                        ZFBPay pay = gson.fromJson(result,ZFBPay.class);
//                        callBack.onPayZFBSuccess(pay.);
                        callBack.onPayZFBSuccess(jsonobject);
                    } else {
                        callBack.onPayZFBError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayZFB", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(37s)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
